import os
import random
import string
import time

import parsel

import sys

sys.path.append("..")

from src import facade
from src.facade import configdicts
from src.mylogger import MLogger
from src.mysqlfacade import MysqlUtiles

abspath = os.path.dirname(os.path.abspath(__file__))
configpath = os.path.join(abspath, "db.ini")


class DownIssueList(object):
    def __init__(self):
        self.logger = MLogger().streamlogger
        self.listdir = configdicts["path"]["path_step6"]
        self.host = configdicts["other"]["host"]
        self.baseurl = "http://" + self.host + "/Kns55/oldnavi/"
        facade.create_dir(self.listdir)
        self.mysqlutile = MysqlUtiles(configpath, "db", logger=self.logger)
        self.header = {
            "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3",
            "Accept-Encoding": "gzip, deflate",
            "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8",
            "Host": self.host,
            "Upgrade-Insecure-Requests": "1",
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36"
        }

    def selectdb(self):
        sql = "select url,baseid,`year`,issue from issue where stat=0 and failcount < 3 limit 1000"
        rows = self.mysqlutile.SelectFromDB(sql)

        for row in rows:
            url = row[0]
            baseid = row[1]
            year = row[2]
            issue = row[3]
            fullurl = self.baseurl+url
            BoolResult, errString, r = facade.BaseRequest(fullurl,
                                                          headers=self.header,
                                                          mark="TitleLeftCell",
                                                          timeout=(30, 60))
            if BoolResult:
                pass
            else:
                pass






